package obecto.component.image
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	
	import obecto.core.FLUIComponent;

	public class Image extends FLUIComponent
	{	
		private var _content : DisplayObject;
		public function get content() : DisplayObject
		{
			return _content;
		}
		
		private var _source : Object;
		[Bindable]
		public function set source(value : Object) : void
		{
			clearOldContent();
			_source = value;
			loadSource(_source);
		}
		public function get source() : Object
		{
			return _source;
		}
		
		[Bindable("loaderInfoChanged")]
		public function get contentLoaderInfo() : LoaderInfo
		{
			return _loader.contentLoaderInfo;
		}
		
		private var _loader : Loader;
		private var _isLoaderComplete : Boolean = false;
		private var _isLoaderUsed : Boolean = false;
		public function Image()
		{
			super();
			
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoaderComplete);
			
			_view = new Sprite();
		}
		
		private function loadSource(source : Object) : void
		{
			var cls : Class;
	        var url : String;
	        var byteArray : ByteArray;
	        
			if (source is Class)
	        {
	            cls = Class(source);
	        }
	        else if (source is String)
	        {
	            try
	            {
	                cls = Class(getDefinitionByName(String(source)));
	            }
	            catch(e:Error)
	            {
	            	url = String(source);
	            }
	        }
	        else if (source is ByteArray)
	        {
	            byteArray = ByteArray(source);
	        }
	        else
	        {
	            throw new Error("Unknown source type.");
	        }
	        
	        if (cls)
	        {
	        	_content = new cls();
	        	addChild(_content);
	        	
	        	dispatchSizeChanged()
	        }
	        
	        if (url)
	        {
	        	_isLoaderUsed = true;
	        	_loader.load(new URLRequest(url));
	        	dispatchEvent(new Event("loaderInfoChanged"));
	        }
	        
	        if (byteArray)
	        {
	        	_isLoaderUsed = true;
	        	_loader.loadBytes(byteArray);
	        	dispatchEvent(new Event("loaderInfoChanged"));
	        }
		}
		
		private function handleLoaderComplete(event : Event) : void
		{
			_isLoaderComplete = true;
			
			_content = _loader.content;
			addChild(_content);
			
			dispatchEvent(new Event(Event.COMPLETE));
			dispatchSizeChanged();
		}
		
		private function clearOldContent() : void
		{
			if (_content)
			{
				removeChild(_content);
				_content = null;
				
				dispatchSizeChanged();
			}
			
			if (_isLoaderUsed)
			{
				if (_isLoaderComplete)
				{
					_loader.unload();
				}
				else
				{
					_loader.close();
				}
			}
			_isLoaderUsed = false;
			_isLoaderComplete = false;
		}
		
		private function dispatchSizeChanged() : void
		{
			dispatchEvent(new Event('sizeChanged'));
		}
	}
}